import groovy.json.*
import org.apache.commons.lang.StringUtils
import redis.clients.jedis.*
import com.alibaba.moco.util.*
import com.alibaba.moco.*
import script.GlobalValues

/**
 * 模拟 滴滴 获取订单详情
 * Created by wb-lj240895 on 2016/11/1.
 */
def excute(request) {
    def slurper = new JsonSlurper()
    def params = request.getParameterMap();
    Jedis redis = RedisPoolInstance.getRedisClient()
    //欢行异常判断
    def e = redis.hget(GlobalValues.KEY_EXCEPTION,"getOrderInfo")
    if (!StringUtils.isBlank(e)) {
        def eJson = slurper.parseText(e)
        return eJson
    }
    GetOrderRequest req = BaseTypeUtil.mapToBean(params, GetOrderRequest.class) as GetOrderRequest
    String token = req.getAccess_token()
    System.out.println(token)
    GetOrderResponse response = new GetOrderResponse()
    if (token != null /* && redis.exists(token)*/) {
        //redis.del(token)
        //判断订单是否存在 存在则更新订单信息
        def orderId = req.getOrder_id();
        if (null != orderId && redis.exists(GlobalValues.KEY_ORDER+orderId)){
            response.setErrno(0)
            response.setErrmsg("SUCCESS")
            /*def data = redis.get(orderId)*/
            def data = redis.get(GlobalValues.KEY_ORDER+ orderId)
            System.out.println(data)
            def map = slurper.parseText(data)
            map.append = "test"
            response.setData(map)
            return JsonOutput.toJson(response)
        }else{
            response.setErrno(-2)
            response.setErrmsg("订单不存在")
            return JsonOutput.toJson(response)
        }
    } else {
        response.setErrno(-1)
        response.setErrmsg("token错误")
        return JsonOutput.toJson(response)
    }
}

public class GetOrderRequest {
    String client_id	//	yes	申请应用时分配的AppKey
    String access_token	//	yes	授权后的access token
    String order_id	//	yes	要确认支付的订单号
    long timestamp	//	yes	当前时间戳
    String sign	//	yes	签名 详细算法参见 签名验证 章节
}

public class GetOrderResponse {
    int errno
    String errmsg
    Map data
}
